<?php

namespace App\Http\Controllers;


use App\Services\OrderService;
use App\Services\pay\AliPayType;
use App\Services\pay\PayServer;
use App\Tools\JsonTools;
use App\Tools\JwtTools;
use Illuminate\Http\Request;

class PayController extends Controller
{

    /**
     * @Notes:调用支付
     * @Author:Planb,
     * @Date: 2022/11/12,
     * @Time: 8:51,
     */
    public function pay(Request $request){
        $uid = JwtTools::transitionId($request->get('token'));
        $houseId = $request->get('house_id');
        $type = $request->get('type');

        if ($uid && $houseId){
            $payResult = (new PayServer())->payType($type,$uid,$houseId);
            if (!$payResult){
                return JsonTools::JsonError([],'10001','支付失败');
            }
        }else{
            return JsonTools::JsonError([],'8001','参数错误');
        }
    }

    /**
     * @Notes:同步回调
     * @Author:Planb,
     * @Date: 2022/11/12,
     * @Time: 10:05,
     */
    public function sync(){
        (new AliPayType())->sync();
    }

    /**
     * @Notes:异步回调
     * @Author:Planb,
     * @Date: 2022/11/12,
     * @Time: 10:05,
     */
    public function notify(){
        $arr = $_POST;

        (new AliPayType())->notify($arr);
    }

    /**
     * @Notes:支付明细
     * @Author:Planb,
     * @Date: 2022/11/15,
     * @Time: 14:57,
     */
    public function payDetail(Request $request){
        $date = $request->get('date');
        $uid = $request->id;
        $payDetail = OrderService::getPayDetail($uid,$date);
        if ($payDetail){
            return JsonTools::JsonSuccess($payDetail,200,'ok');
        }else{
            return JsonTools::JsonError([],9005,'暂无明细数据');
        }
    }
}
